
const { app, pool } = require('./connect')
var path = require('path');
var express = require('express');
app.use('/puclic', express.static('images'));
const user = require('./router/user')
const score = require('./router/score')
const comment = require('./router/comment')
app.all('*', (req, res, next) => {
  //这里处理全局拦截，一定要写在最上面
  next()
})

// 允许跨域
app.all('*', function (req, res, next) {
  console.log(req.headers.origin)
  console.log(req.environ)
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  // res.header("Access-Control-Allow-Origin", '*');
  res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Credentials", "true");
  res.header("X-Powered-By", ' 3.2.1')
  if (req.method === "OPTIONS") res.send(200);/*让options请求快速返回*/
  else next();
});

app.get('/', (req, res) => {  //首页路由
  res.sendFile(__dirname + '/' + 'index.html')
})
app.all('/', (req, res) => {
  pool.getConnection((err, conn) => {
    res.json({ type: 'test' })
    pool.releaseConnection(conn) // 释放连接池，等待别的连接使用
  })
})
app.use('/user', user)
app.use('/score', score)
app.use('/comment', comment)
app.listen(8088, () => {
  console.log('服务启动', 'http://localhost:8088')
})